import { loginApi, getUserApi } from "@/api/user"
import { getToken, setToken, delToken } from "@/utils/storage"
export default {
  state () {
    return {
      token: getToken(),
      userInfo: {}
    }
  },
  mutations: {
    changeToken (state, token) {
      state.token = token
      setToken(token)
    },
    resetToken (state) {
      state.token = ''
      delToken()
    },
    setUser (state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    async asyncLogin ({ commit }, form) {
      const res = await loginApi(form)
      console.log(res, 99887)

      // console.log(res, 99887)
      /* token=》vuex+本地存储 */
      // commit('changeToken', res.data.data.token)
      commit('changeToken', res.data.token)
      /* 获取用户信息 */
      const res2 = await getUserApi(form)
      console.log(res2, 9988)
      commit('setUser', res2.data)
    }
  },
  getters: {},
  namespaced: true
}